package org.nlp2rdf.navigator.client.functionality;

import org.nlp2rdf.navigator.client.AddLabelAndCommentDialogBox;
import org.nlp2rdf.navigator.client.Application;
import org.nlp2rdf.navigator.client.common.ConceptDTO;
import org.nlp2rdf.navigator.client.common.LearnedConceptDTO;
import org.nlp2rdf.navigator.client.common.StoredConceptDTO;
import org.nlp2rdf.navigator.client.util.ConceptDTOHelper;
import org.nlp2rdf.navigator.client.util.PopupHelper;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ConceptWidget extends HorizontalPanel {
	//turns on the debug widget
	public static boolean debug = false; 
	
	final Application app;
	final private VerticalPanel content = new VerticalPanel();
	final private VerticalPanel actionWidgets = new VerticalPanel();
	final private ConceptDTO learningResult;
	final private PopupPanel glass = PopupHelper.getGlass();
	

	FlowPanel html = new FlowPanel();
	Label label = new Label();
	Label accuracy = new Label();
	VerticalPanel moreInfo = new VerticalPanel();

	Anchor loadAction = new Anchor();
	Anchor exportAction = new Anchor();
	Anchor debugAction = new Anchor();

	public Anchor generalAction = new Anchor();
	public Anchor specializeAction = new Anchor();
	public Button match = new Button();

	Button saveToExport = new Button("Save to export");

	public ConceptWidget(final ConceptDTO conceptDTO, Application app) {
		this.app = app;
		this.learningResult = conceptDTO;
		this.setWidth("100%");
		this.content.setWidth("100%");

		// this.learningResult = learningResult;
		// this.setWidget(0, 0, content);
		// this.setWidget(0, 1, actionWidgets);
		this.setHorizontalAlignment(ALIGN_LEFT);
		this.add(content);
		this.setHorizontalAlignment(ALIGN_RIGHT);
		this.add(actionWidgets);

		label = new Label(conceptDTO.getLabelNotNull());
		html = ConceptDTOHelper.getHTMLAsFlowPanel(conceptDTO);
		initMoreInfo(conceptDTO);

		// actions and labels on storedconcepts
		if (conceptDTO instanceof StoredConceptDTO) {
			StoredConceptDTO st = (StoredConceptDTO) conceptDTO;
			initLoadAction(st);
			initExportAction(st);
		}

		// actions and labels on learnedconcepts
		if (conceptDTO instanceof LearnedConceptDTO) {
			LearnedConceptDTO l = (LearnedConceptDTO) conceptDTO;
			String acc = (l.getAccuracy() == 0.0) ? "n/a" : Double.toString(100 * l.getAccuracy()) + "%";
			accuracy = new Label("Acc.: " + acc);
			initSaveToExportAction();
		}

		initDebugAction(conceptDTO);
		debugAction.setVisible(debug);
	}

	public void displayAsRelatedConcept() {

		label.setVisible(true);
		if (learningResult instanceof StoredConceptDTO) {
			actionWidgets.add(loadAction);
			actionWidgets.add(exportAction);
		}

		Tooltip.assignToolTip(label, moreInfo);
		this.content.add(label);
	}

	public void displayAsLearnedConcept() {
		if (learningResult instanceof LearnedConceptDTO) {

			this.content.add(html);
			this.content.add(accuracy);

			actionWidgets.add(specializeAction);
			actionWidgets.add(generalAction);
			actionWidgets.add(match);
			actionWidgets.add(saveToExport);

			if (learningResult instanceof StoredConceptDTO) {
				this.content
						.add(new Label("This concept was created and saved by another user and saved as:"));
				this.content.add(label);
				Tooltip.assignToolTip(label, ConceptDTOHelper.getInfoTooltip(learningResult));
				saveToExport.setEnabled(false);
			}
		}

	}

	private void initMoreInfo(final ConceptDTO learningResult) {
		moreInfo = new VerticalPanel();
		moreInfo.add(new Label("Comment: " + learningResult.getCommentNotNull() + ""));

		if (learningResult instanceof StoredConceptDTO) {
			StoredConceptDTO st = (StoredConceptDTO) learningResult;

			moreInfo.add(ConceptDTOHelper.getHTMLAsFlowPanel(learningResult));
			moreInfo.add(new Label("Number of Views: " + st.getHitcounter()));
			moreInfo.add(new Label("This concept was created and saved by another user."));
			moreInfo.add(new Label(st.getUri()));
		} else if (learningResult instanceof LearnedConceptDTO) {
			moreInfo.add(new Label("This concept was learned by navigating, but not yet saved"));
		} else {
			moreInfo.add(new Label("This concept comes from the original ontology"));
			moreInfo.add(new Label(learningResult.getManchesterSyntaxString()));
		}

	}

	private void initDebugAction(final ConceptDTO learningResult) {
		debugAction = new Anchor("debug");
		actionWidgets.add(debugAction);
		debugAction.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				final DialogBox dialogue = new DialogBox();
				VerticalPanel exportPanel = new VerticalPanel();
				dialogue.add(exportPanel);
				dialogue.center();
				
				Anchor ok = new Anchor("close");
				ok.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogue.hide();
					}
				});
				exportPanel.add(ok);
				exportPanel.add(ConceptDTOHelper.getDebugToolTip(learningResult));
				exportPanel.add(ok);
//				dialogue.showRelativeTo(exportAction);
				
			}
		});
//		Tooltip.assignToolTip(debugAction, ConceptDTOHelper.getDebugToolTip(learningResult));
		
	}

	private void initSaveToExportAction() {
		if (learningResult instanceof LearnedConceptDTO) {
			final LearnedConceptDTO l = (LearnedConceptDTO) learningResult;
			saveToExport.setEnabled(true);
			saveToExport.addClickHandler(new ClickHandler() {

				public void onClick(ClickEvent arg0) {
					glass.show();
					new AddLabelAndCommentDialogBox(l, app, glass).center();

				}
			});
		}
	}

	private void initExportAction(final StoredConceptDTO learningResult) {

		exportAction = new Anchor("export");
		exportAction.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				final DialogBox dialogue = new DialogBox();
				VerticalPanel exportPanel = new VerticalPanel();
				dialogue.add(exportPanel);
				TextArea area = new TextArea();
				 area.setText("coming soon");
				exportPanel.add(area);
				dialogue.showRelativeTo(exportAction);
				Anchor ok = new Anchor("close");
				ok.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						dialogue.hide();
					}
				});
				exportPanel.add(ok);
			}
		});

	}

	private void initLoadAction(final StoredConceptDTO learningResult) {
		loadAction = new Anchor("load");
		loadAction.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				app.displayLearnedConcept(learningResult);
				
				if(learningResult.getExamples()!=null){
					app.loadPosNeg(learningResult.getExamples());
				}
			}
		});
	}

}
